package com.bogewang.apigatewaydynamicfilter;

import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.groovy.GroovyCompiler;
import com.netflix.zuul.groovy.GroovyFileFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@EnableZuulProxy
@EnableConfigurationProperties(FilterConfiguration.class)
@SpringCloudApplication
public class ApiGatewayDynamicFilterApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ApiGatewayDynamicFilterApplication.class).web(true).run(args);
    }

    @Bean
    public FilterLoader filterLoader(FilterConfiguration configuration){
        FilterLoader filterLoader = FilterLoader.getInstance();
        filterLoader.setCompiler(new GroovyCompiler());
        try {
            FilterFileManager.setFilenameFilter(new GroovyFileFilter());
            FilterFileManager.init(
                    configuration.getInterval(),
                    configuration.getRoot() +"/pre",
                    configuration.getRoot() +"/post"
            );
        } catch (Exception e){
            throw  new RuntimeException(e);
        }
        return filterLoader;
    }

}
